package com.sec.android.app.myfiles.module.local.category;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.AbsMyFilesActivity;
import com.sec.android.app.myfiles.info.AppConstants.StoragePath;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.SemFwWrapper;
import com.sec.android.app.myfiles.util.UiUtils;

import java.io.File;

public class CategoryFileRecord extends FileRecord {
    private String mBucketId = null;
    private String mRealFullPath = null;
    private String mFirstItemPath = null;

    public CategoryFileRecord(int id, String path, String name, long size, long date, int fileType, int isHidden, int itemCount, int itemCountHidden) {
        super(StorageType.Category, id, path, name, size, date, fileType, isHidden, itemCount, itemCountHidden);
    }

    public CategoryFileRecord(String fullPath) {
        mStorageType = StorageType.Category;
        mPath = getPath(fullPath);
        mName = getName(fullPath);
        mFileType = FileType.FOLDER;
    }

    public void setBucketId(String bucketId) {
        mBucketId = bucketId;
    }

    public String getBucketId() {
        return mBucketId;
    }

    @Override
    public boolean isRoot() {
        return mBucketId == null;
    }

    public CategoryType getCategoryType() {
        return getCategoryType(super.getFullPath());
    }

    public static String getCategoryPath(CategoryType type) {
        String ret;
        switch (type) {
            case Image:
                ret = StoragePath.CATEGORY_IMAGE;
                break;
            case Video:
                ret = StoragePath.CATEGORY_VIDEO;
                break;
            case Audio:
                ret = StoragePath.CATEGORY_AUDIO;
                break;
            case Document:
                ret = StoragePath.CATEGORY_DOCUMENT;
                break;
            case Apk:
                ret = StoragePath.CATEGORY_APK;
                break;
            default:
                ret = "";
                break;
        }
        return ret;
    }

    public static CategoryType getCategoryType(String categoryPath) {
        CategoryType categoryType = CategoryType.None;
        if (categoryPath != null) {
            if (categoryPath.startsWith(StoragePath.CATEGORY_IMAGE)) {
                categoryType = CategoryType.Image;
            } else if (categoryPath.startsWith(StoragePath.CATEGORY_AUDIO)) {
                categoryType = CategoryType.Audio;
            } else if (categoryPath.startsWith(StoragePath.CATEGORY_VIDEO)) {
                categoryType = CategoryType.Video;
            } else if (categoryPath.startsWith(StoragePath.CATEGORY_DOCUMENT)) {
                categoryType = CategoryType.Document;
            } else if (categoryPath.startsWith(StoragePath.CATEGORY_APK)) {
                categoryType = CategoryType.Apk;
            }
        }
        return categoryType;
    }

    public static String getCategoryName(Context context, CategoryFileRecord record) {
        String ret = "";
        CategoryType type = record.getCategoryType();
        NavigationInfo mNavigationInfo = NavigationManager.getInstance(((AbsMyFilesActivity) context).getProcessId()).getCurInfo();
        Resources res = context.getResources();
        switch (type) {
            case Image:
                ret = res.getString(R.string.images);
                break;
            case Video:
                ret = res.getString(R.string.videos);
                break;
            case Audio:
                ret = res.getString(R.string.audio);
                break;
            case Document:
                ret = res.getString(R.string.documents);
                break;
            case Apk:
                ret = res.getString(UiUtils.getInstallationFileString());
                break;
        }

        if (!record.isRoot() && mNavigationInfo != null && !mNavigationInfo.getIsSearchMode()) {
            ret += "/" + record.getName();
        }

        return ret;
    }

    @Override
    protected int _getId() {
        return mId;
    }

    @Override
    protected Uri _getUri(Context context) {
        return null;
    }

    @Override
    public boolean exists(Context context) {
        boolean bRet;
        if (isRoot()) {
            bRet = true;
        } else {
            File file = SemFwWrapper.file(getFullPath());
            bRet = file.exists();
        }
        return bRet;
    }

    @Override
    public String getDisplayName(Context context) {
        if (isRoot()) {
            return getCategoryName(context, this);
        } else {
            return getName();
        }
    }

    @Override
    public boolean equals(Object o) {
        boolean bRet = false;

        if (o instanceof CategoryFileRecord) {
            CategoryFileRecord cmp = (CategoryFileRecord) o;

            bRet = (cmp.getCategoryType() == getCategoryType() &&
                    ((getBucketId() == null && cmp.getBucketId() == null) || (cmp.getBucketId() == getBucketId() || (getBucketId() != null && getBucketId().equals(cmp.getBucketId())))));

        } else {
            bRet = super.equals(o);
        }
        return bRet;
    }

    public String getRealPath() {
        return mRealFullPath;
    }

    public void setRealPath(String realPath) {
        mRealFullPath = realPath;
    }

    public String getFirstItemPath() {
        return mFirstItemPath;
    }

    public void setFirstItemPath(String firstItemPath) {
        mFirstItemPath = firstItemPath;
    }

    @Override
    public String getFullPath() {
        if (isRoot()) {
            return super.getFullPath();
        } else {
            return mRealFullPath;
        }
    }

    @Override
    public int hashCode() {
        int initValue = 631; // randomly chosen prime numbers
        int constant = 2971; // randomly chosen prime numbers

        int nRet = initValue;
        nRet += mStorageType.hashCode() * constant;
        if (mPath != null) {
            nRet += mPath.hashCode() * constant;
        }

        if (mRealFullPath != null) {
            nRet += mRealFullPath.hashCode() * constant;
        }

        return nRet;
    }
}
